package com.htjx.mall.controllers.commonapi;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.util.WebUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.htjx.mall.commons.models.OrderGroup;
import com.htjx.mall.commons.models.enums.OrderStatus;


/**
 * 
 * @author Administrator
 */
public class CommonCookie {
	
	private static String URLEncode (String input) {
		try {
			return java.net.URLEncoder.encode(input,"utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return input;
	}
	
	private static String URLDecode (String input) {
		try {
			return java.net.URLDecoder.decode(input,"utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return input;
		
	}
	
	public static void setCartToCookie(Cart cart, HttpServletResponse response) {
		String cartJson = URLEncode(JSON.toJSONString(cart));
		
		Cookie cookie = new Cookie(Constant.CART_NAME, cartJson);
		cookie.setMaxAge(60*60*24);
		cookie.setPath("/");
		response.addCookie(cookie);
	}
	
	public static Cart getCartFromCookie(HttpServletRequest request) {
		Cookie cookie = WebUtils.getCookie(request, Constant.CART_NAME);
		if (cookie == null) {
			Cart cart = new Cart();
			return cart;
		} else {
			return (Cart)JSON.parseObject(URLDecode(cookie.getValue()), new TypeReference<Cart>() {});
		}
	}
	
	public static void setOrderIds(List<Long> orderIds, HttpServletResponse response) {
		String json = JSON.toJSONString(orderIds);
		Cookie cookie = new Cookie(Constant.ORDER_LIST, json);
		cookie.setMaxAge(60*60*24);
		cookie.setPath("/");
		response.addCookie(cookie);
	}
	
	public static List<Long> getOrderIds(HttpServletRequest request) {
		Cookie cookie = WebUtils.getCookie(request, Constant.ORDER_LIST);
		if (cookie == null) {
			return null;
		} else {
			return (List<Long>)JSON.parseObject(cookie.getValue(), new TypeReference<List<Long>>() {});
		}
	}
	
	public static void setOrderGroup(OrderGroup orderGroup, HttpServletResponse response) {
		String json = URLEncode(JSON.toJSONString(orderGroup));
		Cookie cookie = new Cookie(Constant.ORDER_GROUP, json);
		cookie.setMaxAge(60*60*24);
		cookie.setPath("/");
		response.addCookie(cookie);
	}
	
	public static OrderGroup getOrderGroup(HttpServletRequest request) {
		Cookie cookie = WebUtils.getCookie(request, Constant.ORDER_GROUP);
		if (cookie == null) {
			return null;
		} else {
			return (OrderGroup)JSON.parseObject(URLDecode(cookie.getValue()), new TypeReference<OrderGroup>() {});
		}
	}
	
	public static void setOrderStatus(OrderStatus orderStatus, HttpServletResponse response) {
		String json = orderStatus.name();
		Cookie cookie = new Cookie(Constant.ORDER_STATUS, json);
		cookie.setMaxAge(60*60*24);
		cookie.setPath("/");
		response.addCookie(cookie);
	}
	
	public static OrderStatus getOrderStatus(HttpServletRequest request) {
		Cookie cookie = WebUtils.getCookie(request, Constant.ORDER_STATUS);
		if (cookie == null) {
			return null;
		} else {
			return OrderStatus.valueOf(cookie.getValue());
		}
	}
	
	public static void setAlipayCallback(Map<String, String> params, HttpServletResponse response) {
		String json = URLEncode(JSON.toJSONString(params));
		Cookie cookie = new Cookie(Constant.ALIPAY_CALLBACK, json);
		cookie.setMaxAge(60*60*24);
		cookie.setPath("/");
		response.addCookie(cookie);
	}
	
	public static Map<String, String> getAlipayCallback(HttpServletRequest request) {
		Cookie cookie = WebUtils.getCookie(request, Constant.ALIPAY_CALLBACK);
		if (cookie == null) {
			return null;
		} else {
			return (Map<String, String>)JSON.parseObject(URLDecode(cookie.getValue()), new TypeReference<Map<String, String>>() {});
		}
	}
	
	public static void setAlipayNotify(Map<String, String> params, HttpServletResponse response) {
		String json = URLEncode(JSON.toJSONString(params));
		Cookie cookie = new Cookie(Constant.ALIPAY_NOTIFY, json);
		cookie.setMaxAge(60*60*24);
		cookie.setPath("/");
		response.addCookie(cookie);
	}
	
	public static Map<String, String> getAlipayNotify(HttpServletRequest request) {
		Cookie cookie = WebUtils.getCookie(request, Constant.ALIPAY_NOTIFY);
		if (cookie == null) {
			return null;
		} else {
			return (Map<String, String>)JSON.parseObject(URLDecode(cookie.getValue()), new TypeReference<Map<String, String>>() {});
		}
	}
}
